/**
 * 面试题57-1：和为s的两个数字
 */
public class Offer_57_I {
    /**
     * 双指针
     * <p>
     * 时间复杂度：O(n)
     * <p>
     * 空间复杂度：O(1)
     */
    public int[] twoSum(int[] nums, int target) {
        int[] ans = new int[2];
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int sum = nums[left] + nums[right];
            if (sum > target) {
                right--;
            } else if (sum < target) {
                left++;
            } else {
                ans[0] = nums[left];
                ans[1] = nums[right];
                break;
            }
        }
        return ans;
    }
}
